/*
 * @lc app=leetcode.cn id=25 lang=cpp
 *
 * [25] K 个一组翻转链表
 */

// @lc code=start
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    ListNode* successor;
    ListNode* reversrN(ListNode* head,int n)
    {
        if(n == 1)
        {
            successor = head->next ;
            return head;
        }
        ListNode* last = reversrN(head->next,n-1);
        
        head->next->next = head;
        head->next = successor;
        return last;
    }

    
    ListNode* reverseKGroup(ListNode* head, int k) {
        if(head == NULL ||  k == 0)
            return head;
        ListNode* p = head;
        ListNode* res = NULL;
        for(int i = 0; i < k ;i++)      
        {
            if(p == NULL)
                return head;
            p = p->next;
        }   
        res = reversrN(head,k);
        head->next = reverseKGroup(p,k);
        return res;
    }

};
// @lc code=end

